import requests
from bs4 import BeautifulSoup
import time


#获取内容   存放地址在：D:\\movie\\
def getContent(url):
    try:
        agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
        session=requests.session()
        response=session.get(url=url,headers={"User-Agent":agent})
        response.raise_for_status()
        response.encoding=response.apparent_encoding
    except Exception as e:
        print("爬取错误")
    else:
        print("爬取成功")
        response.close()
        return response.content.decode("utf-8")

#下载
#--httpList 视频下载地址
#--titleList 视频名
#--path 存盘地址
def download_Video(httpList,titleList):
    agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    path='D:\\movie\\'

    num=0
    for i in httpList:
        # filePath='Dtest.mp4'
        videoName=path+titleList[num]+'.mp4'
        print(videoName)
        response=requests.get(url=i,headers={"User-Agent":agent},stream=True)
        time.sleep(1)
        with open(videoName,'wb') as f:
            for chunk in response.iter_content(chunk_size=1024):
                f.write(chunk)
        f.close()
        count=num+1
        print(titleList[num]+"-----已经下载完成"+repr(count)+"个任务-----正在进行下一任务----")
        response.close()
        num += 1
    print("全部下载完成")

#解析网页
def parserContent(htmlContent,url):
    try:
        soup=BeautifulSoup(htmlContent,"html.parser")
        #补齐格式 print(soup.prettify())
        ulList=soup.find("ul",class_="content-list")
        liList=ulList.find_all("li",class_="content-item")
        #存放视频名
        titleList=[]
        #存放视频网页链接名
        linkList=[]
        # 下载地址
        httpList = []
        #主域名
        str=url
        #-------------------短视频跳转具体页面----------------------------
        #--linkList  视频网页地址
        #--titleList 视频名
        num=0
        for i in liList:
            linkList.append(str + i.a.get('href'))
            titleList.append(i.a.get('title'))
            #开始请求,num控制条数
            htmlContent = getContent(linkList[num])
            num += 1
            soup = BeautifulSoup(htmlContent, "html.parser")
            trList = soup.find("tr", class_="app_hide")
            inputList = trList.findAll("input")
            #一共有两条下载地址【0】为mp4，【1】为迅雷
            httpList.append(inputList[0].get("data-clipboard-text"))
        download_Video(httpList, titleList)
    except Exception as e:
        print("请复制原网址："+url+"在浏览器中打开，复制最新网址粘贴至主函数URL中即可使用")

if __name__ == '__main__':

    #主网址
    url='https://www.5eb862f80092.com/'
    index='/index/home.html'
    #第一次请求获得session
    indexUrl=url+index
    htmlContent=getContent(indexUrl)
    #第二次请求短视频
    duan='/shipin/list-短视频.html'
    duanUrl=url+duan
    htmlContent2 = getContent(duanUrl)
    #解析短视频页面,并将网址传进去为下个页面做准备                下次优化网址拼接
    parserContent(htmlContent2,url)


